package com.eastfair.venueservice.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.annotation.annotation.log.SysLog;
import com.eastfair.annotation.annotation.security.PreAuth;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.boot.request.PageParams;
import com.eastfair.core.base.R;
import com.eastfair.echo.core.EchoService;
import com.eastfair.venueservice.dto.WorkOvertimeRuleDTO;
import com.eastfair.venueservice.dto.WorkOvertimeRulePageQuery;
import com.eastfair.venueservice.dto.WorkOvertimeRuleSaveDTO;
import com.eastfair.venueservice.dto.WorkOvertimeRuleUpdateDTO;
import com.eastfair.venueservice.entity.WorkOvertimeRule;
import com.eastfair.venueservice.enumeration.WorkOvertimeRuleOvertimeTypeEnum;
import com.eastfair.venueservice.service.WorkOvertimeRuleService;
import com.eastfair.venueservice.vo.WorkOvertimeRuleVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 加班规则
 * </p>
 *
 * @author dqq
 * @date 2023 -05-05
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/workOvertimeRule")
@Api(value = "WorkOvertimeRule", tags = "加班规则")
@PreAuth(replace = "venueservice:workOvertimeRule:")
public class WorkOvertimeRuleController extends SuperController<WorkOvertimeRuleService, Long, WorkOvertimeRule, WorkOvertimeRulePageQuery, WorkOvertimeRuleSaveDTO, WorkOvertimeRuleUpdateDTO> {

    @Resource
    private EchoService echoService;

    @Resource
    private WorkOvertimeRuleService workOvertimeRuleService;

    @Override
    public void handlerResult(IPage<WorkOvertimeRule> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list) {
        List<WorkOvertimeRule> workOvertimeRuleList = list
                .stream()
                .map((map) -> WorkOvertimeRule.builder().build())
                .collect(Collectors.toList());
        return R.success(baseService.saveBatch(workOvertimeRuleList));
    }

    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<WorkOvertimeRulePageQuery> params) {
        WorkOvertimeRulePageQuery pageQuery = params.getModel();
    }

    /**
     * 查询加班规则列表VO.
     *
     * @param workOvertimeRuleDTO the work overtime rule dto
     * @return the r
     */
    @PostMapping("/query/workOvertimeRule")
    public R<List<WorkOvertimeRuleVO>> queryWorkOvertimeRule(@RequestBody WorkOvertimeRuleDTO workOvertimeRuleDTO) {
        log.info("queryWorkOvertimeRule - 查询加班规则列表VO, workOvertimeRuleDTO={}", workOvertimeRuleDTO);
        return R.successDef(workOvertimeRuleService.listWorkOvertimeRuleVO(workOvertimeRuleDTO));
    }

    /**
     * id查询加班规则.
     *
     * @param id the id
     * @return the by id
     */
    @GetMapping("/get/{id}")
    public R<WorkOvertimeRuleVO> getById(@PathVariable("id") Long id) {
        log.info("getById - id查询加班规则, id={}", id);
        return R.successDef(workOvertimeRuleService.getVOById(id));
    }

    /**
     * 加班类型查询场馆设置的加班规则.
     *
     * @param overtimeType the overtime type
     * @return the by venue overtime type
     */
    @GetMapping("/get/venue/overtimeType/{overtimeType}")
    public R<WorkOvertimeRuleVO> getByVenueOvertimeType(@PathVariable("overtimeType") WorkOvertimeRuleOvertimeTypeEnum overtimeType) {
        log.info("getByVenueOvertimeType - 加班类型查询场馆设置的加班规则, overtimeType={}", overtimeType);
        return R.successDef(workOvertimeRuleService.getVOByVenueOvertimeType(overtimeType));
    }

    /**
     * 加班类型查询主场设置的加班规则.
     *
     * @param overtimeType       the overtime type
     * @param exhibitionManageId the exhibition manage id
     * @param homeId             the home id
     * @return the by home overtime type
     */
    @GetMapping("/get/home/overtimeType")
    public R<WorkOvertimeRuleVO> getByHomeOvertimeType(@RequestParam("overtimeType") WorkOvertimeRuleOvertimeTypeEnum overtimeType,
                                                       @RequestParam("exhibitionManageId") Long exhibitionManageId,
                                                       @RequestParam("homeId") Long homeId) {
        log.info("getByHomeOvertimeType - 加班类型查询主场设置的加班规则, overtimeType={}, exhibitionManageId={}, homeId={}", overtimeType, exhibitionManageId, homeId);
        return R.successDef(workOvertimeRuleService.getVOByHomeOvertimeType(overtimeType, exhibitionManageId, homeId));
    }

    /**
     * 场馆保存加班规则.
     *
     * @param ruleDTO the rule dto
     * @return the r
     */
    @SysLog("场馆配置加班规则")
    @PostMapping("/save/venue")
    public R<Boolean> saveForVenue(@RequestBody WorkOvertimeRuleDTO ruleDTO) {
        log.info("saveForVenue - 场馆保存加班规则, ruleDTO={}", ruleDTO);
        return R.successDef(workOvertimeRuleService.saveForVenue(ruleDTO));
    }

    /**
     * 主场配置加班规则.
     *
     * @param ruleDTO the rule dto
     * @return the r
     */
    @SysLog("主场配置加班规则")
    @PostMapping("/save/home")
    public R<Boolean> saveForHome(@RequestBody WorkOvertimeRuleDTO ruleDTO) {
        log.info("saveForHome - 主场配置加班规则, ruleDTO={}", ruleDTO);
        return R.successDef(workOvertimeRuleService.saveForHome(ruleDTO));
    }

    /**
     * 修改加班规则.
     *
     * @param ruleDTO the rule dto
     * @return the r
     */
    @SysLog("修改加班规则")
    @PostMapping("/update")
    public R<WorkOvertimeRule> updateWorkOvertimeRule(@RequestBody WorkOvertimeRuleDTO ruleDTO) {
        log.info("updateWorkOvertimeRule - 修改保存加班规则, ruleDTO={}", ruleDTO);
        return R.successDef(workOvertimeRuleService.updateWorkOvertimeRule(ruleDTO));
    }

    /**
     * 场馆更新加班规则
     *
     * @param ruleDTO the rule dto
     * @return the r
     */
    @SysLog("场馆修改加班规则")
    @PostMapping("/venue/update")
    public R<WorkOvertimeRule> updateVenueWorkOvertimeRule(@RequestBody WorkOvertimeRuleDTO ruleDTO) {
        log.info("updateVenueWorkOvertimeRule - 场馆更新加班规则, ruleDTO={}", ruleDTO);
        return R.successDef(workOvertimeRuleService.updateVenueWorkOvertimeRule(ruleDTO));
    }
}
